package com.zhaoc.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.zhaoc.domain.Customer;
import com.zhaoc.domain.FixedArea;
import com.zhaoc.service.IFixAreaService;
import com.zhaoc.web.base.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>title:com.zhaoc.web.action</p>
 * date:2018/8/3 0003
 * time:11:47
 * author:赵成
 * user:13566
 */

/**
 * 定区管理
 */
@ParentPackage("json-default")
public class FixedAreaAction extends BaseAction<FixedArea> {
    //注入service
    @Autowired
    private IFixAreaService fixAreaService;

    //保存定区
    @Action(value = "fixedArea_save", results = {@Result(name = SUCCESS,
            location = "./pages/base/fixed_area.html", type = "redirect")})
    public String save() {
        //调用业务层保存定区
        System.out.println(model);
        fixAreaService.save(model);
        return SUCCESS;
    }

    //分页查询定区
    @Action(value = "fixedArea_pageQuery", results = {@Result(name = "success", type = "json")})
    public String pageQuery() {
        //构造分页查询对象
        Pageable pageable = new PageRequest(page - 1, rows);
        //构造条件查询对象
        Specification<FixedArea> specification = new Specification<FixedArea>() {
            @Override
            public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //存放查询条件
                List<Predicate> list = new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(model.getId())) {
                    Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
                    list.add(p1);
                }
                if (StringUtils.isNotBlank(model.getCompany())) {
                    Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getId() + "%");
                    list.add(p2);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };

        //调用service
        Page<FixedArea> pageData = fixAreaService.findPageDate(specification, pageable);
        //将查询到的数据压入栈顶
        pushPageDataToValueStack(pageData);

        return SUCCESS;
    }

    //查询未关联分区列表
    @Action(value = "fixedarea_findNoAssociationCustomers", results = {@Result(name = SUCCESS, type = "json")})
    public String fixedAreaNoAssociationCustomer() {

        //使用webCliect调用webService接口
        Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
                .accept(MediaType.APPLICATION_JSON)
                .getCollection(Customer.class);
        //将查到的数据输入值栈
        ActionContext.getContext().getValueStack().push(collection);

        return SUCCESS;
    }

    //查询已关联分区列表
    @Action(value = "fixedarea_findHasAssociationFixedareaCustomers"
            , results = {@Result(name = SUCCESS, type = "json")})
    public String findHasAssociationFixedareaCustomers() {

        //使用webCliect调用webService接口
        Collection<? extends Customer> collection =
                WebClient.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"+model.getId())
                .accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
                .getCollection(Customer.class);
        //将查到的数据输入值栈
        ActionContext.getContext().getValueStack().push(collection);

        return SUCCESS;
    }



    //使用属性驱动
    private String[] customerIds;

    public void setCustomerIds(String[] customerIds) {
        this.customerIds = customerIds;
    }

    //关联客户到定区
    @Action(value = "decidedzone_assigncustomerstodecidedzone",
            results = {@Result(name = SUCCESS,type = "redirect",
                    location = "./pages/base/fixed_area.html")})
    public String assigncustomerstodecidedzone(){

        //将接收到的ID转换成字符串
        String join = StringUtils.join(customerIds, ",");

        //想crm发送修改请求
        WebClient.create("http://localhost:9002/crm_management/services/customerService/associationCustomersToFixedarea?customersIdStr="
                +join+"&fixedAreaId="+model.getId()).put(null);

        return SUCCESS;
    }

    //使用属性驱动
    private Integer courierId;
    private Integer takeTimeId;

    public void setCourierId(Integer courierId) {
        this.courierId = courierId;
    }

    public void setTakeTimeId(Integer takeTimeId) {
        this.takeTimeId = takeTimeId;
    }

    //关联快递员到定区
    @Action(value = "fixedArea_associationCourierToFixedArea"
            ,results = {@Result(name = SUCCESS,type = "redirect"
            ,location = "./pages/base/fixed_area.html")})
    public String associationCourierToFixedArea(){

        //调用service层关联快递员
        fixAreaService.associactionCourierTofixedArea(model.getId(),courierId,takeTimeId);

        return SUCCESS;
    }
}
